#!/usr/bin/perl -wT
# -*- Mode: perl; indent-tabs-mode: nil -*-
#
# The contents of this file are subject to the Mozilla Public
# License Version 1.1 (the "License"); you may not use this file
# except in compliance with the License. You may obtain a copy of
# the License at http://www.mozilla.org/MPL/
#
# Software distributed under the License is distributed on an "AS
# IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
# implied. See the License for the specific language governing
# rights and limitations under the License.
#
# The Original Code is the Bugzilla Bug Tracking System.
# 
# The Initial Developer of the Original Code is Netscape Communications
# Corporation. Portions created by Netscape are Copyright (C) 1998
# Netscape Communications Corporation. All Rights Reserved.
# 
# Contributor(s): TomWij <TomWij@live.com>

##############################################################################
#
# emptyDatabase.cgi
# ---------------------
# Empties the tests from the database prior to testing and prepares a new empty model.
#
##############################################################################

# Connect to the MySQL database.
use DBI;
use DBD::mysql;

$db_name = 'bugs';
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';

$driver = DBI->install_driver("mysql"); 
$connect = $driver->connect("DBI:mysql:database=$db_name;host=$db_host", $db_user, $db_pass, {}) or die "MySQL: Connection failed";

# Get the id of the Test component.
$query = "SELECT id, product_id FROM components where name = 'TestComponent';";
$query_handle = $connect->prepare($query) or die "<b>MySQL: Query Preparation failed.</b>";
$query_handle->execute() or die "<b>MySQL: Query Execution failed.</b>";
$query_handle->bind_columns(undef, \$cid, \$pid);
$query_handle->fetch();

# Remove all the bugs that we have used in the past for testing.
$query = "DELETE FROM bugs WHERE component_id = '$cid';";
$query_handle = $connect->prepare($query) or die "<b>MySQL: Query Preparation failed.</b>";
$query_handle->execute() or die "<b>MySQL: Query Execution failed.</b>";

# Remove all the statistics that have been stored related to testing.
$query = "DELETE FROM suggestions WHERE cid = 'TestComponent';";
$query_handle = $connect->prepare($query) or die "<b>MySQL: Query Preparation failed.</b>";
$query_handle->execute() or die "<b>MySQL: Query Execution failed.</b>";

# Remove the model that has been used for testing.
unlink("../data/bugs-$pid-$cid-model.arff");

# Recreate a new empty model.
use LWP::UserAgent;
my $ua = new LWP::UserAgent;
$ua->post('http://localhost/extensions/BugPrediction/lib/trainClassifier.cgi', { pid => $pid, cid => $cid});

$product = $pid;
$component = $cid;